package com.bawei.day0129.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Toast;

import com.bawei.day0129.Bean.LogBean;
import com.bawei.day0129.R;
import com.bawei.day0129.base.BaseActivity;
import com.bawei.day0129.databinding.ActivityLoginBinding;
import com.bawei.day0129.utils.RsaCoder;
import com.bawei.day0129.utils.SPUtils;
import com.bawei.day0129.viewmodel.LoginViewModel;

public class LoginActivity extends BaseActivity<LoginViewModel, ActivityLoginBinding> {


    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        dataBinding.xql.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(dataBinding.loginpwd.getTransformationMethod()==null){
                    dataBinding.loginpwd.setTransformationMethod(new PasswordTransformationMethod());
                    dataBinding.xql.setImageResource(R.mipmap.login_icon_hide_password_n);
                }else {
                    dataBinding.loginpwd.setTransformationMethod(null);
                    dataBinding.xql.setImageResource(R.mipmap.login_icon_show_password);
                }
            }
        });
        dataBinding.loginBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String s = dataBinding.loginName.getText().toString();
                final String s1 = dataBinding.loginpwd.getText().toString();
                String tiles="^\\w+([-+.]\\w+)*@qq.com$";
                if(TextUtils.isEmpty(s)){
                    Toast.makeText(LoginActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(s1)){
                    Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!s.matches(tiles)){
                    Toast.makeText(LoginActivity.this, "邮箱格式有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    String s2 = RsaCoder.encryptByPublicKey(s1);
                    viewModel.getGetLogin(s,s2).observe(LoginActivity.this, new Observer<LogBean>() {
                        @Override
                        public void onChanged(LogBean logBean) {
                            if (logBean.getMessage().equals("登录成功")){
                                Toast.makeText(LoginActivity.this, logBean.getMessage(), Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, logBean.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
        dataBinding.zhuche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,ZhucActivity.class));
            }
        });
    }
}